package com.tsy.server.system.controller;

import com.tsy.server.common.Result;
import com.tsy.server.system.entity.dto.ResetPasswordDTO;
import com.tsy.server.system.entity.dto.UserLoginDTO;
import com.tsy.server.system.entity.dto.UserRegisterDTO;
import com.tsy.server.system.entity.dto.VerifyCodeDTO;
import com.tsy.server.system.entity.vo.UserLoginVO;
import com.tsy.server.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户认证控制器
 */
@Api(tags = "用户认证接口", description = "包含用户注册、登录、获取用户信息、退出登录和发送邮箱验证码等功能")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    /**
     * 用户注册
     *
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @ApiOperation(value = "用户注册", notes = "用户注册接口，需要提供用户名、密码、确认密码、邮箱、验证码和昵称")
    @PostMapping("/register")
    public Result<Void> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        boolean success = userService.register(registerDTO);
        if (success) {
            return Result.success("注册成功");
        }
        return Result.failed("注册失败");
    }

    /**
     * 用户登录
     *
     * @param loginDTO 登录信息
     * @return 登录结果
     */
    @ApiOperation(value = "用户登录", notes = "用户登录接口，需要提供用户名和密码，登录成功后返回token信息")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        UserLoginVO loginVO = userService.login(loginDTO);
        return Result.success(loginVO, "登录成功");
    }


    /**
     * 退出登录
     *
     * @param request 请求
     * @return 退出结果
     */
    @ApiOperation(value = "退出登录", notes = "退出登录接口，需要在请求头中携带token")
    @PostMapping("/logout")
    public Result<Void> logout(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
            boolean success = userService.logout(token);
            if (success) {
                return Result.success("退出成功");
            }
        }
        return Result.failed("退出失败");
    }

    /**
     * 发送邮箱验证码
     *
     * @param email 邮箱
     * @return 发送结果
     */
    @ApiOperation(value = "发送邮箱验证码", notes = "发送邮箱验证码接口，用于用户注册或重置密码时获取验证码")
    @ApiImplicitParam(name = "email", value = "邮箱地址", required = true, dataType = "String", paramType = "path")
    @GetMapping("/code/{email}")
    public Result<Void> sendVerificationCode(@PathVariable String email) {
        boolean success = userService.sendVerificationCode(email);
        if (success) {
            return Result.success("验证码已发送，请查收邮件");
        }
        return Result.failed("验证码发送失败");
    }

    /**
     * 验证邮箱验证码
     *
     * @param verifyCodeDTO 验证码信息
     * @return 验证结果
     */
    @ApiOperation(value = "验证邮箱验证码", notes = "验证邮箱验证码接口，用于验证用户输入的验证码是否正确")
    @PostMapping("/verifyCode")
    public Result<Map<String, String>> verifyEmailCode(@Validated @RequestBody VerifyCodeDTO verifyCodeDTO) {
        String restPwdToken = userService.verifyEmailCode(verifyCodeDTO);
        Map<String, String> map = new HashMap<>();
        map.put("restPwdToken", restPwdToken);
        return Result.success(map);
    }

    /**
     * 忘记密码-重置密码
     *
     * @param resetPasswordDTO 重置密码信息
     * @return 重置结果
     */
    @ApiOperation(value = "忘记密码-重置密码", notes = "忘记密码-重置密码接口，需要提供邮箱、验证码、新密码和确认新密码")
    @PostMapping("/resetPwd")
    public Result<Void> resetPassword(@Validated @RequestBody ResetPasswordDTO resetPasswordDTO) {
        userService.resetPassword(resetPasswordDTO);
        return Result.success("密码重置成功");
    }

}